﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://www.codeplex.com/EmlenMud
using System.Text;
using System.Runtime.Serialization;
using System;
using System.Threading;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using BrainTechLLC.ThreadSafeObjects;
using WordsMatching;
using System.Xml.Serialization;
using System.Collections.Specialized;
#if USE_HYPER
using Hyper.ComponentModel;
#endif

namespace BrainTechLLC
{
    [Serializable]
    [DataContract]
#if NO_SILVERLIGHT
    //[TypeDescriptionProvider(typeof(HyperTypeDescriptionProvider))]
#endif
    public class BasePropertyChangeSupport : INotifyPropertyChanged, INotifyPropertyChanging, INotifyCollectionChanged
    {
        [field: NonSerialized, XmlIgnore]
        public event PropertyChangedEventHandler PropertyChanged;

#if NO_SILVERLIGHT
        [field: NonSerialized, XmlIgnore]
        public event PropertyChangingEventHandler PropertyChanging;

        [field: NonSerialized, XmlIgnore]
        public event NotifyCollectionChangedEventHandler CollectionChanged;

        public void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (CollectionChanged != null)
                CollectionChanged(sender, e);
        }

#else
        [field: NonSerialized, XmlIgnore]
        public event EventHandler<PropertyChangingEventArgsEx> PropertyChanging;

        [field: NonSerialized, XmlIgnore]
		public event EventHandler<NotifyCollectionChangedEventArgsEx<IdentifierAndValue>> CollectionChanged;

        public void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgsEx<IdentifierAndValue> e)
        {
            if (CollectionChanged != null)
                CollectionChanged(sender, e);
        }
#endif

        public virtual void OnPropertyChanged(string property, object oldValue, object newValue)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgsEx(property) { OldValue = oldValue, NewValue = newValue });
        }

        public virtual void OnPropertyChanging(string property, object oldValue, object newValue)
        {
            if (PropertyChanging != null)
                PropertyChanging(this, new PropertyChangingEventArgsEx(property) { OldValue = oldValue, NewValue = newValue });
        }

        public virtual void OnPropertyChanged(object sender, PropertyChangedEventArgsEx args)
        {
            if (PropertyChanged != null)
            {
                args.ChildObject = sender;
                PropertyChanged(this, args);
            }
        }

        public virtual void OnPropertyChanging(object sender, PropertyChangingEventArgsEx args)
        {
            if (PropertyChanging != null)
            {
                args.ChildObject = sender;
                PropertyChanging(this, args);
            }
        }
    }
}